package cn.myframe.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.myframe.controller.AbstractController;
import cn.myframe.entity.sys.SysMessageEntity;
import cn.myframe.service.sys.SysMessageService;
import cn.myframe.service.sys.SysUserService;
import cn.myframe.utils.core.R;
import cn.myframe.utils.annotation.IgnoreAuth;

/**
 * 系统用户
 * 
 * @author ljh
 * @email 
 * @date 2017年09月04日 上午10:40:10
 */
@RestController
@RequestMapping("/sys/sysMessage")
public class SysMessageController extends AbstractController {
    @Autowired
    private SysMessageService sysMessageService;
    /**
     * 状态 - 待提交审核
     */
    public static final int STATUS_SUBMIT_AUDIT = 0;
    
    /**
     * 状态 - 待审核通过
     */
    public static final int STATUS_AUDIT_PASS = 1;
    
    public static final int ADMIN = 1;
	@Autowired
	private SysUserService sysUserService;
    
    /**
     * 
     * @param request
     * @param modelMap
     * @return
     */	
	@RequestMapping(value = "/messageList")
	@IgnoreAuth
	public R getMessage(HttpServletRequest request,ModelMap modelMap){
			List<SysMessageEntity> msgList = new ArrayList<SysMessageEntity>();
            Integer watingSubmit = 0;//待提交审核
            Integer watingPass = 0;//待审核通过
            Integer dBcount = 0;
            String auditSubmit = "auditSubmit";//提交审核
            String auditPass = "auditPass";//审核通过
            //String inject = "inject";
            //String broadcast = "broadcast";//播发
        	String moduleName = "";
        	List<Long> userscol = null;
        	Integer isHas = null;//是否拥有该权限       	
        	Map<Integer, List<Long>> users = new HashMap<Integer, List<Long>>();
        	//（1、红包管理；2、优惠券管理；3、活动管理；4、素材管理；5、模板管理；6、portal管理；7、投放管理；8、投放策略管理）
        	List<Long> list1 =	sysUserService.queryDataUser(getUser().getUserId(), "busredpacket:list", getUser().getCompanyNo());
        	List<Long> list2 =	sysUserService.queryDataUser(getUser().getUserId(), "buswxcardmp:list", getUser().getCompanyNo());
        	List<Long> list3 =	sysUserService.queryDataUser(getUser().getUserId(), "busactivity:list", getUser().getCompanyNo());
        	List<Long> list4 =	sysUserService.queryDataUser(getUser().getUserId(), "advmaterial:list", getUser().getCompanyNo());
        	List<Long> list5 =	sysUserService.queryDataUser(getUser().getUserId(), "advtemplate:list", getUser().getCompanyNo());
			List<Long> list6 =	sysUserService.queryDataUser(getUser().getUserId(), "advrpportal:list", getUser().getCompanyNo());
			List<Long> list7 =	sysUserService.queryDataUser(getUser().getUserId(), "advschedule:list", getUser().getCompanyNo());
			List<Long> list8 =	sysUserService.queryDataUser(getUser().getUserId(), "advdispatchstrategy:list", getUser().getCompanyNo());
        	users.put(1, list1);
        	users.put(2, list2);
        	users.put(3, list3);
        	users.put(4, list4);
        	users.put(5, list5);
			users.put(6, list6);
			users.put(7, list7);
			users.put(8, list8);
		Map<Integer,String> moduleNames = new LinkedHashMap<Integer,String>();
            moduleNames.put(1, "busredpacket:");
            moduleNames.put(2, "buswxcardmp:");
            moduleNames.put(3, "busactivity:");
            moduleNames.put(4, "advmaterial:");
            moduleNames.put(5, "advtemplate:");
			moduleNames.put(6, "advrpportal:");
			moduleNames.put(7, "advschedule:");
			moduleNames.put(8, "advdispatchstrategy:");
            if(getUser().getType()!= null && getUser().getType() ==ADMIN){
            	for(Integer key:moduleNames.keySet()){
            		Map<String, Integer> resultMap = new LinkedHashMap<String, Integer>();
            		SysMessageEntity message = new SysMessageEntity();
            		moduleName = moduleNames.get(key);
            		message.setModuleName(moduleName);
            		message.setModuleId(key);//用于sql查询时的判断
            		message.setStatus(STATUS_SUBMIT_AUDIT);
            		watingSubmit = sysMessageService.selectForMsg(message,null);//根据状态值去查询出待审核条数
            		dBcount = dBcount + watingSubmit;
            		message.setWaitSubmit(watingSubmit);
            		resultMap.put(moduleName + STATUS_SUBMIT_AUDIT, watingSubmit);//将结果存入结果Map
            		message.setStatus(STATUS_AUDIT_PASS);
            		watingPass = sysMessageService.selectForMsg(message,null);
            		message.setWaitPass(watingPass);
            		dBcount = dBcount + watingPass;
            		resultMap.put(moduleName + STATUS_AUDIT_PASS, watingPass);
            		message.setMap(resultMap);
            		msgList.add(message);
            	}
            	
            }else{
            	for(Integer key:moduleNames.keySet()){
            		Map<String, Integer> resultMap = new LinkedHashMap<String, Integer>();
            		SysMessageEntity message = new SysMessageEntity();
            		moduleName = moduleNames.get(key);
            		userscol = users.get(key);
            		message.setModuleName(moduleName);
            		message.setModuleId(key);
            		//判断用户是否具有审核权限
            		isHas = sysMessageService.checkAuthority(message, auditSubmit, getUserId());
            		if(isHas != null && isHas > 0){   
            			//具有相关模块的审核权限
            			message.setStatus(STATUS_SUBMIT_AUDIT);
            			watingSubmit = sysMessageService.selectForMsg(message,userscol);//根据状态值去查询出待审核条数
            			resultMap.put(moduleName + STATUS_SUBMIT_AUDIT, watingPass);
            		}else{
            			//没有相关模块的审核权限
            			watingSubmit = 0;
            		}
            		message.setWaitSubmit(watingSubmit);
            		dBcount = dBcount + watingSubmit;
            		//将结果存入结果Map
            		//resultMap.put(moduleName + StateReverse.STATUS_SUBMIT_AUDIT, watingPass);
            		
            		//判断用户是否拥有审核通过权限
/*            		if(moduleName.equals("advschedule:")){
            			//广告投放只有播发权限，没有启用权限，所以做区分
            			//isHas = sysMessageService.checkAuthority(message, broadcast, operatorNo);
            		}else if(moduleName.equals("advmaterial:")){
            			//isHas = sysMessageService.checkAuthority(message, inject, operatorNo);            			

            		}else{
            			isHas = sysMessageService.checkAuthority(message, auditPass, getUserId());            			
            		}*/
            		isHas = sysMessageService.checkAuthority(message, auditPass, getUserId());       
            		if(isHas != null && isHas > 0){
            			//具有相关模块启用权限
                		message.setStatus(STATUS_AUDIT_PASS);
                		watingPass = sysMessageService.selectForMsg(message,userscol);  
                		resultMap.put(moduleName + STATUS_AUDIT_PASS, watingPass);
            		}else{
            			//没有相关模块的审核权限
            			watingPass = 0;
            		}
            		dBcount = dBcount + watingPass;
            		message.setWaitPass(watingPass);
            		message.setMap(resultMap);
            		msgList.add(message);
            	}
            	
            }
            String resultMsg ="";
           // System.out.println(dBcount);
            resultMsg = com.alibaba.fastjson.JSON.toJSONString(msgList);
           // System.out.println(resultMsg);


		return R.ok().put("msgList", resultMsg).put("dBcount", dBcount);
	}
	

	
}
